package com.yzcrm.erp.eqxiu.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yzcrm.erp.eqxiu.entity.EqxiuEventRegister;
import com.yzcrm.erp.eqxiu.entity.EqxiuVisits;
import com.yzcrm.utils.R;

/**
 * @author jiang.xue
 */
public class EqxiuVisitService {


    /**
     * 访问
     * @param jsonObject
     * @return
     */
    @Before(Tx.class)
    public R addVisit(JSONObject jsonObject) {

//        Log.getLog(KmsBbsService.class).info(jsonObject.toString() + "....");

        EqxiuVisits eqxiuVisits = jsonObject.getObject("entity", EqxiuVisits.class);
        if(ObjectUtil.isNull(eqxiuVisits)) {
            R.error("参数不正确");
        }

        eqxiuVisits.setEnterTime(DateUtil.date());
        eqxiuVisits.setCreateTime(DateUtil.date());
        eqxiuVisits.setUpdateTime(DateUtil.date());
        eqxiuVisits.setStatus(1);
        if(ObjectUtil.isNotEmpty(jsonObject.getJSONObject("entity").getString("spreadUserId"))) {
            eqxiuVisits.setOwnerUserId(Long.valueOf(jsonObject.getJSONObject("entity").getString("spreadUserId")));
        }

        boolean flag = eqxiuVisits.save();

        return flag ? R.ok() : R.error();
    }

    /**
     * 注册
     * @param jsonObject
     * @return
     */
    @Before(Tx.class)
    public R addRegister(JSONObject jsonObject) {

//        Log.getLog(KmsBbsService.class).info(jsonObject.toString() + "....");

        EqxiuEventRegister eqxiuEventRegister = jsonObject.getObject("entity", EqxiuEventRegister.class);
        if(ObjectUtil.isNull(eqxiuEventRegister)) {
            R.error("参数不正确");
        }

        eqxiuEventRegister.setCreateTime(DateUtil.date());
        eqxiuEventRegister.setUpdateTime(DateUtil.date());
        eqxiuEventRegister.setCheckStatus(1);
        eqxiuEventRegister.setStatus(1);
        if(ObjectUtil.isNotEmpty(jsonObject.getJSONObject("entity").getString("spreadUserId"))) {
            eqxiuEventRegister.setOwnerUserId(Long.valueOf(jsonObject.getJSONObject("entity").getString("spreadUserId")));
        }

        boolean flag = eqxiuEventRegister.save();

        return flag ? R.ok() : R.error();
    }


}
